/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package clientereligadora.paineis;

import clientereligadora.entidades.Medidor;
import clientereligadora.operacoes.Operacao;
import clientereligadora.tableModel.MedidorTableModel;
import java.awt.event.KeyEvent;
import java.util.List;
import javax.swing.SwingConstants;
import javax.swing.table.DefaultTableCellRenderer;

/**
 *
 * @author Leandro Aquino
 */
public class PainelMedidor extends javax.swing.JPanel {

    List<Medidor> listaMedidores;
    MedidorTableModel medidorTableModel;
    Operacao operacao = new Operacao();
    String filtro = "";

    /**
     * Creates new form PainelMedidor
     */
    public PainelMedidor() {
        listaMedidores = operacao.getListaMedidores(filtro);
        medidorTableModel = new MedidorTableModel(listaMedidores);
        initComponents();
         setParTableMedidores();///
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jPanel3 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        tbl_medidor = new javax.swing.JTable(medidorTableModel);
        txt_codReligadora = new javax.swing.JTextField();
        jLabel35 = new javax.swing.JLabel();
        jButton8 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();

        jPanel1.setBackground(new java.awt.Color(255, 255, 255));
        jPanel1.setPreferredSize(new java.awt.Dimension(742, 489));

        jPanel3.setBackground(new java.awt.Color(255, 255, 255));
        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder(""));
        jPanel3.setPreferredSize(new java.awt.Dimension(722, 459));

        tbl_medidor.setAutoCreateColumnsFromModel(false);
        tbl_medidor.setAutoCreateRowSorter(true);
        tbl_medidor.setFont(new java.awt.Font("Arial", 0, 11)); // NOI18N
        tbl_medidor.getTableHeader().setReorderingAllowed(false);
        jScrollPane2.setViewportView(tbl_medidor);

        txt_codReligadora.setFont(new java.awt.Font("Arial", 0, 11)); // NOI18N
        txt_codReligadora.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                txt_codReligadoraKeyPressed(evt);
            }
        });

        jLabel35.setFont(new java.awt.Font("Arial", 0, 11)); // NOI18N
        jLabel35.setForeground(new java.awt.Color(0, 0, 71));
        jLabel35.setText("RELIGADORA:");

        jButton8.setFont(new java.awt.Font("Arial", 0, 11)); // NOI18N
        jButton8.setForeground(new java.awt.Color(0, 0, 71));
        jButton8.setText("LISTAR MEDIDORES");
        jButton8.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton8ActionPerformed(evt);
            }
        });

        jButton3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagens/14_alterar.png"))); // NOI18N
        jButton3.setBorder(null);
        jButton3.setBorderPainted(false);
        jButton3.setContentAreaFilled(false);
        jButton3.setFocusPainted(false);

        jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagens/27 -Add.png"))); // NOI18N
        jButton1.setBorder(null);
        jButton1.setBorderPainted(false);
        jButton1.setContentAreaFilled(false);
        jButton1.setFocusPainted(false);
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton4.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagens/26 -delete.png"))); // NOI18N
        jButton4.setBorder(null);
        jButton4.setBorderPainted(false);
        jButton4.setContentAreaFilled(false);
        jButton4.setFocusPainted(false);

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 781, Short.MAX_VALUE)
                            .addGroup(jPanel3Layout.createSequentialGroup()
                                .addComponent(jLabel35)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(txt_codReligadora, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(jButton8)))
                        .addGap(5, 5, 5))
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(jButton1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jButton3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jButton4)
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGap(22, 22, 22)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel35)
                    .addComponent(txt_codReligadora, javax.swing.GroupLayout.PREFERRED_SIZE, 17, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton8, javax.swing.GroupLayout.PREFERRED_SIZE, 17, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 23, Short.MAX_VALUE)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 317, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jButton1)
                    .addComponent(jButton3)
                    .addComponent(jButton4))
                .addContainerGap(23, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addGap(11, 11, 11)
                .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, 800, Short.MAX_VALUE)
                .addGap(11, 11, 11))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap(13, Short.MAX_VALUE)
                .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, 461, Short.MAX_VALUE)
                .addContainerGap(15, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 822, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents

    private void txt_codReligadoraKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txt_codReligadoraKeyPressed

        int key = evt.getKeyCode();
        if (key == KeyEvent.VK_ENTER) {
            int codigo =  Integer.parseInt(txt_codReligadora.getText());
            System.out.println(codigo);
            localizaMedidor(codigo);
        }
    }//GEN-LAST:event_txt_codReligadoraKeyPressed

    private void jButton8ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton8ActionPerformed

        filtro = "";
        listaMedidores = operacao.getListaMedidores(filtro);
        medidorTableModel = new MedidorTableModel(listaMedidores);

        tbl_medidor.setModel(medidorTableModel);
         setParTableMedidores();        // TODO add your handling code here:
    }//GEN-LAST:event_jButton8ActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jButton1ActionPerformed

     public void localizaMedidor(int cod){

        filtro = "WHERE medidor.id = "+cod+"";
        
        listaMedidores = operacao.getListaMedidores(filtro);
        medidorTableModel = new MedidorTableModel(listaMedidores);
        
        tbl_medidor.setModel(medidorTableModel);
        setParTableMedidores();
    }
    public void setParTableMedidores(){
        DefaultTableCellRenderer centralizado = new DefaultTableCellRenderer();
        centralizado.setHorizontalAlignment(SwingConstants.CENTER);

       
        
        
        tbl_medidor.getColumnModel().getColumn(0).setCellRenderer(centralizado);

  
        tbl_medidor.getColumnModel().getColumn(1).setCellRenderer(centralizado);

      
        tbl_medidor.getColumnModel().getColumn(2).setCellRenderer(centralizado);

     
        tbl_medidor.getColumnModel().getColumn(3).setCellRenderer(centralizado);

    
        tbl_medidor.getColumnModel().getColumn(4).setCellRenderer(centralizado);

    
        tbl_medidor.getColumnModel().getColumn(5).setCellRenderer(centralizado);

  
        tbl_medidor.getColumnModel().getColumn(6).setCellRenderer(centralizado);

  
        tbl_medidor.getColumnModel().getColumn(7).setCellRenderer(centralizado);

 
        tbl_medidor.getColumnModel().getColumn(8).setCellRenderer(centralizado);

      
        tbl_medidor.getColumnModel().getColumn(9).setCellRenderer(centralizado);

      
        tbl_medidor.getColumnModel().getColumn(10).setCellRenderer(centralizado);

 
        tbl_medidor.getColumnModel().getColumn(11).setCellRenderer(centralizado);

  
        tbl_medidor.getColumnModel().getColumn(12).setCellRenderer(centralizado);

 
        tbl_medidor.getColumnModel().getColumn(13).setCellRenderer(centralizado);

       
        tbl_medidor.getColumnModel().getColumn(14).setCellRenderer(centralizado);

     
        tbl_medidor.getColumnModel().getColumn(15).setCellRenderer(centralizado);

      
        tbl_medidor.getColumnModel().getColumn(16).setCellRenderer(centralizado);

 
        tbl_medidor.getColumnModel().getColumn(17).setCellRenderer(centralizado);

    
        tbl_medidor.getColumnModel().getColumn(18).setCellRenderer(centralizado);

     
        tbl_medidor.getColumnModel().getColumn(19).setCellRenderer(centralizado);

  
        tbl_medidor.getColumnModel().getColumn(20).setCellRenderer(centralizado);
        
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JButton jButton8;
    private javax.swing.JLabel jLabel35;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable tbl_medidor;
    private javax.swing.JTextField txt_codReligadora;
    // End of variables declaration//GEN-END:variables
}
